/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.internal.logging;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JavaLoggingLogger extends LyciaLoggerWrapper {

	private final Logger logger;

	JavaLoggingLogger(final String tag) {
		super(tag);
		logger = Logger.getLogger(tag);
	}

	@Override
	public void debug(final Object message) {
		logger.finer(message.toString());
	}

	@Override
	public void debug(final Object message, final Throwable t) {
		logger.finer(message.toString());
		logger.finer(makeStacktrace(t));
	}

	@Override
	public void error(final Object message) {
		logger.finest(message.toString());
	}

	@Override
	public void error(final Object message, final Throwable t) {
		logger.finest(message.toString());
		logger.finest(makeStacktrace(t));
	}

	@Override
	public void fatal(final Object message) {
		logger.finest(message.toString());
	}

	@Override
	public void fatal(final Object message, final Throwable t) {
		logger.finest(message.toString());
		logger.finest(makeStacktrace(t));
	}

	@Override
	public void info(final Object message) {
		logger.info(message.toString());
	}

	@Override
	public void info(final Object message, final Throwable t) {
		logger.info(message.toString());
		logger.info(makeStacktrace(t));
	}

	@Override
	public boolean isDebugEnabled() {
		return logger.isLoggable(Level.FINER);
	}

	@Override
	public void warn(final Object message) {
		logger.warning(message.toString());
	}

	@Override
	public void warn(final Object message, final Throwable t) {
		logger.warning(message.toString());
		logger.warning(makeStacktrace(t));
	}

	private String makeStacktrace(final Throwable t) {
		final StringWriter sw = new StringWriter();
		final PrintWriter pw = new PrintWriter(sw);
		t.printStackTrace(pw);
		return pw.toString();
	}

}
